package com.demo.netty.http;

import com.demo.netty.simple.NettyServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author gy
 * @date 2022/6/3
 */
public class TestServer {
    public static void main(String[] args) throws Exception{
        // 创建 BossGroup 和 WorkerGroup
        /*
        1. 创建两个线程组 bossGroup workerGroup
        2. bossGroup 只是处理连接请求 ，真正的和客户端业务处理，会交给
        workerGroup 完成
        3. 两个都是无限循环
        4. bossGroup 和 workerGroup 含有的子线程（NioEventLoop）的个数
            默认实际 cpu核数 * 2
         */
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

            // 创建服务器端的启动对象，配置参数
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)//设置两个线程组
                    .channel(NioServerSocketChannel.class) // 使用 NioServerSocketChannel 作为服务器的通道实现
                    .childHandler(new TestServerInitializer());

            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            // 对通道关闭进行异步监听
            channelFuture.channel().closeFuture().sync();

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

}
